var express = require('express')
var router = express.Router()
var connection = require('../db/sql.js')
var user = require('../db/userSql.js')
const { name } = require('ejs')
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' })
})

//修改密码
router.post('/api/modify', function (req, res, next) {
  let params = {
    userTel: req.body.phone,
    userPwd: req.body.pwd,
  }
  //查询用户是否存在
  connection.query(user.queryUserTel(params), function (error, results) {
    //某一条记录数id
    let id = results[0].id
    let pwd = results[0].pwd
    connection.query(
      `update user set pwd = replace(pwd,'${pwd}','${params.userPwd}') where id = ${id}`,
      function (err, result) {
        res.send({
          code: 200,
          data: {
            success: true,
            msg: '修改成功',
          },
        })
      },
    )
  })
})

//注册
router.post('/api/register', function (req, res, next) {
  let params = {
    userTel: req.body.phone,
    userPwd: req.body.pwd,
  }
  //查询用户是否存在
  connection.query(user.queryUserTel(params), function (error, results) {
    if (error) throw error
    //用户存在
    if (results.length > 0) {
      res.send({
        code: 200,
        data: {
          success: true,
          msg: '用户已存在，请登录！',
          data: results[0],
        },
      })
    } else {
      //不存在，新增一条数据
      connection.query(user.inserData(params), function (err, result) {
        connection.query(user.queryUserTel(params), function (e, r) {
          res.send({
            code: 200,
            data: {
              success: true,
              msg: '注册成功！登录成功',
              data: r[0],
            },
          })
        })
      })
    }
  })
})

//手机号密码登录
router.post('/api/login', function (req, res, next) {
  //接收用户传递的手机号和密码
  let params = {
    userTel: req.body.userTel,
    userPwd: req.body.userPwd,
  }
  //查询用户手机号
  connection.query(user.queryUserTel(params), function (error, results) {
    //手机号存在
    if (results.length > 0) {
      connection.query(user.queryUserPwd(params), function (err, result) {
        if (result.length > 0) {
          //手机号密码正确
          res.send({
            code: 200,
            data: {
              msg: '登录成功',
              success: true,
              data: result[0],
            },
          })
        } else {
          //密码不对
          res.send({
            code: 302,
            data: {
              msg: '密码不对',
              success: false,
            },
          })
        }
      })
    } else {
      //手机号不存在
      res.send({
        code: 301,
        data: {
          msg: '手机号不存在',
          success: false,
        },
      })
    }
  })
})

//通过商品id获取商品详情
router.get('/api/goods/id', function (req, res, next) {
  let id = req.query.id
  connection.query('select * from goodslist where id=' + id + '', function (error, results) {
    if (error) throw error
    res.json({
      code: 0,
      data: results[0],
    })
  })
})

//分类接口
router.get('/api/goods/list', function (req, res, next) {
  res.send({
    code: 0,
    data: [
      {
        //一级数据（左侧）
        id: 0,
        name: '正在流行',
        data: [
          {
            id: 0,
            name: '9.9包邮',
            imgUrl: '/public/image/9.9.jpeg',
          },
          {
            id: 1,
            name: '热销爆款',
            imgUrl: '/public/image/爆款.png',
          },
          {
            id: 2,
            name: '玫瑰',
            imgUrl: '/public/image/玫瑰.png',
          },
        ],
      },
      {
        id: 1,
        name: '玫瑰',
        data: [
          {
            id: 0,
            name: '玫瑰',
            imgUrl: '/public/image/玫瑰.png',
          },
          {
            id: 1,
            name: '热销爆款',
            imgUrl: '/public/image/爆款.png',
          },
        ],
      },
    ],
  })
})

router.get('/api/goods/shopList', function (req, res, next) {
  const searchName = req.query.searchName
  connection.query(
    'SELECT * FROM goodslist WHERE name LIKE ?',
    ['%' + searchName + '%'],
    function (error, results) {
      if (error) {
        console.error('Database query error: ', error)
        return res.status(500).send({
          code: 0,
          message: '服务器内部错误',
        })
      }
      res.send({
        code: 1,
        data: results,
      })
    },
  )
})

//首页数据
router.get('/api/index', function (req, res, next) {
  res.send({
    code: 0,
    data: {
      cartList: [
        {
          id: 1,
          img: 'https://img1.baidu.com/it/u=1772458925,3413010708&fm=253&fmt=auto?w=500&h=500',
          title: '植物大全',
        },
        {
          id: 2,
          img: 'https://img2.baidu.com/it/u=1030951222,2252347369&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
          title: '大型盆栽',
        },
        {
          id: 3,
          img: 'http://t15.baidu.com/it/u=2146261718,2728764275&fm=224&app=112&f=JPEG?w=483&h=500',
          title: '中型盆栽',
        },
        {
          id: 4,
          img: 'http://t14.baidu.com/it/u=1846020977,116899158&fm=224&app=112&f=JPEG?w=350&h=350',
          title: '小型盆栽',
        },
        {
          id: 5,
          img: 'https://t13.baidu.com/it/u=4115216821,3381512181&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1737824400&t=db55ecc366e558e3d23605bf026b0f84',
          title: '鲜花花艺',
        },
        {
          id: 6,
          img: 'https://img0.baidu.com/it/u=1801115510,510052292&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1737824400&t=43a88dc450bafe7a1ed9ba79e56457d0',
          title: '多肉植物',
        },
        {
          id: 6,
          img: 'https://t14.baidu.com/it/u=4282700625,1498364516&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1737824400&t=28398e1ee5b489fec571e8600ea04fcf',
          title: '菜籽',
        },
        {
          id: 6,
          img: 'https://img1.baidu.com/it/u=3846422059,3517810840&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500',
          title: '园艺用品',
        },
      ],
      listArr: [
        {
          id: 1,
          name: '云南新鲜玫瑰花鲜花速递同城配送基地直发插花家用生日花束顺丰',
          actor: '/public/image/image.png',
          actorname: '初心如一',
          actortag: '爆单新品',
          price: '199',
          sales: '999+',
          imgUrl: '/public/image/image.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 2,
          name: '洛神玫瑰花鲜花云南基地直发教师节礼物2024新款花束家用插花鲜切',
          actor: '/public/image/鲜花-洛神.png',
          actorname: '小甜心',
          actortag: '爆单女王',
          price: '135',
          sales: '999+',
          imgUrl: '/public/image/鲜花-洛神.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 3,
          name: '多头泡泡卡布奇诺巧克力小玫瑰鲜花花束云南基地直发鲜切花年货节',
          actor: '/public/image/多头泡泡-鲜花.png',
          actorname: '小甜心',
          actortag: '爆单女王',
          price: '39.9',
          sales: '200+',
          imgUrl: '/public/image/多头泡泡-鲜花.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 4,
          name: '腊梅花鲜花水养鲜切花干枝蜡梅鲜花年宵花卉水培植物花束客厅摆设',
          actor: '/public/image/腊梅花-鲜花.png',
          actorname: '小甜心',
          actortag: '爆单女王',
          price: '100.9',
          sales: '700+',
          imgUrl: '/public/image/腊梅花-鲜花.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 5,
          name: '家用卉水插花洛神直发切养花鲜花植物枝基地客厅水培云南鲜玫瑰花',
          actor: '/public/image/水培云南鲜玫瑰花.png',
          actorname: '小甜心',
          actortag: '爆单女王',
          price: '36.9',
          sales: '500+',
          imgUrl: '/public/image/水培云南鲜玫瑰花.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 6,
          name: '小盼菩提盆栽带果菩提树老桩盆景办公室客厅桌面植物室内禅意摆件',
          actor: '/public/image/小盼菩提盆栽.png',
          actorname: '金粉世家',
          actortag: '爆单女王',
          price: '9.9',
          sales: '300+',
          imgUrl: '/public/image/小盼菩提盆栽.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 7,
          name: '竹柏盆景迷你小盆栽驱蚊水培植物花卉办公室内桌面禅意绿植微景观',
          actor: '/public/image/竹柏盆景.png',
          actorname: '小甜心',
          actortag: '爆单女王',
          price: '19.9',
          sales: '350+',
          imgUrl: '/public/image/竹柏盆景.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 8,
          name: '小金豆盆栽新中式盆景金豆树苗带果桌面禅意绿植室内好养易活四季',
          actor: '/public/image/金豆.png',
          actorname: '菩提家',
          actortag: '爆单女王',
          price: '32.8',
          sales: '364+',
          imgUrl: '/public/image/金豆.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        {
          id: 9,
          name: '松土神器不锈钢耙子松土耙园艺工具压土耙耙土除草耙盆景工具耙子',
          actor: '/public/image/松土神器.png',
          actorname: '神奇喵生活家居',
          actortag: '爆单款',
          price: '153.96',
          sales: '316+',
          imgUrl: '/public/image/松土神器.png',
          // leftUrl:
          //   'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
          rightUrl: '/public/image/zhibo.png',
          byUrl: '/public/image/baoyou.png',
        },
        // {
        //   id: 5,
        //   name: '甜美各式小夹子刘海小发卡',
        //   actor: 'https://img01.hua.com/uploadpic/newpic/9012709.jpg_220x240.jpg',
        //   actorname: '小甜心',
        //   actortag: '爆单女王',
        //   price: '199',
        //   sales: 500,
        //   imgUrl: 'http://images.jjjux.com/20230915114544_49119.jpg',
        //   leftUrl:
        //     'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
        //   rightUrl:
        //     'https://img2.baidu.com/it/u=3092446475,2772055955&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        //   byUrl:
        //     'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
        // },
        // {
        //   id: 6,
        //   name: '甜美各式小夹子刘海小发卡',
        //   actor: './images/s-6.webp',
        //   actorname: '小甜心',
        //   actortag: '爆单女王',
        //   price: '199',
        //   sales: 500,
        //   imgUrl: 'https://img02.hua.com/zhuanti/valentine/2025/9012676.png?a1',
        //   leftUrl:
        //     'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
        //   rightUrl:
        //     'https://img2.baidu.com/it/u=3092446475,2772055955&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        //   byUrl:
        //     'http://t14.baidu.com/it/u=1739560370,1441849695&fm=224&app=112&f=JPEG?w=500&h=500',
        // },
      ],
    },
  })
})
module.exports = router
